package elasticsearch.util;

import org.elasticsearch.action.bulk.BulkItemResponse;
import org.elasticsearch.action.bulk.BulkRequestBuilder;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.get.MultiGetItemResponse;
import org.elasticsearch.action.get.MultiGetResponse;
import org.elasticsearch.client.transport.TransportClient;

import java.io.IOException;
import java.util.Iterator;

import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;


/**
 * 查询文档
 * @author jiangbang
 * @date 2017/7/27
 */
public class EsGetUtil {
    private static final String index = "index1";
    private static final String type = "type1";
    /**
     * 通过文档ID获取索引
     * @param client
     */
    public static GetResponse getIndex(TransportClient client,String id) {
        GetResponse response = client.prepareGet(index,type,id).get();
        return response;
    }

    /**
     * 通过ID查询多条数据
     * @param client
     */
    public static void multiGetResponse (TransportClient client) {
        MultiGetResponse multiGetItemResponses = client.prepareMultiGet()
                .add(index,type, "1")
                .add(index, type, "2", "3", "4")
                .get();

        for (MultiGetItemResponse itemResponse : multiGetItemResponses) {
            GetResponse response = itemResponse.getResponse();
            if (response.isExists()) {
                String json = response.getSourceAsString();
                System.out.println(json);
                System.out.println("----------------\n");
            }
        }
    }
    /**
     * 通过ID查询多条数据
     * @param client
     */
    public static void buik (TransportClient client) {
        try{
            BulkRequestBuilder bulkRequest = client.prepareBulk();
            bulkRequest.add(client.prepareIndex(index, type, "1")
                    .setSource(jsonBuilder()
                            .startObject()
                            .field("first_name", "jiang2")
//                            .field("postDate", new Date())
//                            .field("message", "trying out Elasticsearch")
                            .endObject()
                    )
            );
            bulkRequest.add(client.prepareIndex(index, type, "3")
                    .setSource(jsonBuilder()
                            .startObject()
                            .field("first_name", "liu")
//                            .field("postDate", new Date())
//                            .field("message", "another post")
                            .endObject()
                    )
            );
            BulkResponse bulkResponse = bulkRequest.get();
            if (!bulkResponse.hasFailures()) {
                Iterator<BulkItemResponse> bulkItemResponseIterator = bulkResponse.iterator();
                while (bulkItemResponseIterator.hasNext()){
                    BulkItemResponse bulkItemResponse= bulkItemResponseIterator.next();
                }
            }
        }catch (IOException e){
            e.printStackTrace();
        }
    }

}
